package com.rongzhong.routing.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import jxl.Cell;
import jxl.Sheet;
import net.sf.json.JSONObject;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rongzhong.routing.hibernate.HibernateUtil;

/**
 * 
 * @author longxuegang@gmail.com
 * @version 1.0 2011-9-22
 * @since 1.0
 * @see
 */

public class PerformanceUtil {
	
	private HibernateUtil hibernate = HibernateUtil.getInstance();
	private Configuration configure = Configuration.getInstance();
	private static final Logger log = LoggerFactory.getLogger(PerformanceUtil.class);
	private static PerformanceUtil instance = new PerformanceUtil();

	private final int startPositionRowID = 3;
	private final int startPositionColumnID = 0;
	
	public String getDBHostPort(int dbIndex){
		return configure.getDBHostPort(dbIndex);
	}
	
	public boolean isMaster(int dbIndex){
		return configure.getMasterDBIndex() == dbIndex ? true : false;
	}
	
	public int getDbCheckNumber() {
		return configure.getDatabaseCheckNum();
	} 
	
	public String doubleFormat(String format,double value){
		format = (format == null || format == "") ? "0.00" : format;
		DecimalFormat deformat = new DecimalFormat(format);
		return deformat.format(value);
	}
	
	public int getStartPositionColumnId(Sheet rs,String reference){
		if (rs == null) {
			log.error("sheet can't be load . It's null");
		}
		Cell[] cells = rs.getRow(startPositionRowID);
		for (int i = 0; i < cells.length; i++) {
			String content = cells[i].getContents(); 
			if (content.contains(reference)) {
				return i + 1;
			}else if(content.contains(getToday())){
				return i;
			}
		}
		return cells.length + 1;
	}
	
	public int getStartPositionRowId(Sheet rs,String reference) {
		if (rs == null) {
			log.error("sheet can't be load . It's null");
		}
		Cell[] cells = rs.getColumn(startPositionColumnID);
		for (int i = 0; i < cells.length; i++) {
			String content = cells[i].getContents(); 
			if (content.contains(reference)) {
				return i;
			}
		}
		return 0;
	}
	
	public String getLastReviewDate() {
		Date now = new Date();
		Date yesterday = new Date(now.getTime() - 24*3600*1000);
		return new SimpleDateFormat("yyyy-MM-dd").format(yesterday.getTime());
	}
	
	public String getToday() {
		Date now = new Date();
		return new SimpleDateFormat("yyyy-MM-dd").format(now.getTime());
	}

	public Object select(int dbIndex,String sql){
		Session session = null;
		try {
			session = hibernate.currentSession(dbIndex);
			SQLQuery query = session.createSQLQuery(sql);
			return query.uniqueResult();
		} catch (Exception e) {
			log.error("load from db run into error",e);
			return null;
		}finally{
			hibernate.closeSession(session);
		}
	}
	
	public Object selectArray(int dbIndex,String sql){
		return select(dbIndex,sql);
	}
	
	public JSONObject initJson(String[] columns,Object[] result) {
		JSONObject json = new JSONObject();
		if (columns == null || columns.length <= 0) {
			return json;
		}
		if (result == null) {
			for (int i = 0; i < columns.length; i++) {
				json.put(columns[i], 0);
			}
			return json;
		}
		int length = result.length == columns.length ? result.length : 0;
		for (int j = 0; j < length; j++) {
			json.put(columns[j], result[j]);
		}
		return json;
	}
	
	public double divide(double divisor, double dividend) {
		if (dividend == 0) {
			return 0;
		}
		return divisor /dividend;
	}
	
	public static PerformanceUtil getInstance(){
		return instance;
	}
}
